package com.cnap.service.impl;

import cn.hutool.http.HttpResponse;
import com.cnap.CnapApplicationTests;
import com.cnap.model.log.OperationLog;
import com.cnap.model.log.OperationTypeEnum;
import com.cnap.service.OperationLogService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <类文件描述>
 *
 * @author xc
 **/
class OperationLogServiceImplTest extends CnapApplicationTests {

    @Autowired
    private OperationLogService operationLogService;


    @Test
    void saveOperationLogWithoutOK() {
        HttpResponse response = Mockito.mock(HttpResponse.class);
        Mockito.when(response.isOk()).thenReturn(false);

        Mockito.when(mockRequest.execute()).thenReturn(response);


        boolean result = operationLogService.saveOperationLog(Mockito.mock(OperationLog.class));
        Assertions.assertFalse(result);
    }

    @Test
    void testSaveOperationLog() {
        String responseStr = "{\n" +
                "\t\"code\": 200,\n" +
                "\t\"message\": \"OK\",\n" +
                "\t\"data\": \"success\"\n" +
                "}";
        HttpResponse response = Mockito.mock(HttpResponse.class);
        Mockito.when(response.isOk()).thenReturn(true);
        Mockito.when(response.body()).thenReturn(responseStr);

        Mockito.when(mockRequest.execute()).thenReturn(response);

        boolean result = operationLogService.saveOperationLog(OperationTypeEnum.LOGIN.getType(), "log message");
        Assertions.assertTrue(result);
    }

    @Test
    void testSaveOperationLogWithCode500() {
        String responseStr = "{\n" +
                "\t\"code\": 500,\n" +
                "\t\"message\": \"fail\",\n" +
                "\t\"data\": null\n" +
                "}";
        HttpResponse response = Mockito.mock(HttpResponse.class);
        Mockito.when(response.isOk()).thenReturn(true);
        Mockito.when(response.body()).thenReturn(responseStr);

        Mockito.when(mockRequest.execute()).thenReturn(response);


        boolean result = operationLogService.saveOperationLog(Mockito.mock(OperationLog.class));
        Assertions.assertFalse(result);
    }
}